package exam.axhu.action;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import exam.axhu.entity.Choice;
import exam.axhu.entity.Course;
import exam.axhu.entity.CourseInfo;
import exam.axhu.entity.ExamPaper;
import exam.axhu.entity.Student;
import exam.axhu.entity.Teacher;
import exam.axhu.entity.User;
import exam.axhu.service.CourseService;
import exam.axhu.service.StudentService;
import exam.axhu.service.TeacherService;


@SuppressWarnings("serial")
@Controller @Scope("prototype")
public class Login extends ActionSupport{
//defaultClass: teacherLogin
	@Resource TeacherService teacherService;
	@Resource StudentService studentService;
	@Resource CourseService courseService;
	
	private User user;
	private Teacher teacher;
	private Student student;
	public User getUser() {
		return user;
	}
	public void setUser(User user) {
		this.user = user;
	}
	public Student getStudent() {
		return student;
	}
	public void setStudent(Student student) {
		this.student = student;
	}
	public Teacher getTeacher() {
		return teacher;
	}
	public void setTeacher(Teacher teacher) {
		this.teacher = teacher;
	}
	
	public String login(){
		if(user.getUserID()==null|| user.getPassword()==null||user.getType()==null)
		{
			addActionError("用户名、密码、姓名、身份不能为空!");
			return "LoginFailed";
		}
		if(user.getType().equals("T"))
		{
			teacher=new Teacher(user.getUserID(),user.getName(),user.getPassword());
			if(teacherService.find(teacher.getTeacherID())!=null)
			{
				if (teacher.getPassword().equals(teacherService.find(teacher.getTeacherID()).getPassword()))
				{
					//ActionContext.getContext().put("realTeacher", teacherService.list());
					ActionContext.getContext().getSession().put("teacherInfo", teacherService.find(teacher.getTeacherID()));
					ActionContext.getContext().put("courses",courseService.findByTeacherID(teacher.getTeacherID()));
					ActionContext.getContext().getSession().put("teacherID", teacherService.find(teacher.getTeacherID()).getTeacherID());
					ActionContext.getContext().getSession().put("name", teacherService.find(teacher.getTeacherID()).getName());
					return "TeacherLoginSucceed";
				}
				else{
					addActionMessage("密码错误!请重新输入密码");
					return "LoginFailed";
				}
			}
			else
			{   
				if(teacherService.find(teacher.getTeacherID())==null)
					addActionError("不存在此用户!");
				return "LoginFailed";
			}
		}
		
		else{
			student=new Student(user.getUserID(),user.getName(),user.getPassword());
			if(studentService.find(student.getStudentID())!=null)
			{
				if (student.getPassword().equals(studentService.find(student.getStudentID()).getPassword()))
				{
					ActionContext.getContext().getSession().put("studentInfo", studentService.find(student.getStudentID()));
					List<Course> ems = courseService.listCoursesByStudentID(student.getStudentID());
					List<CourseInfo> courses = new ArrayList<CourseInfo>();
					if(ems!=null)
						for(Course em : ems)
							courses.add(courseService.findByCourseID(em.getCourseID()));
					ActionContext.getContext().put("courses",courses);
					ActionContext.getContext().getSession().put("studentID", studentService.find(student.getStudentID()).getStudentID());
					ActionContext.getContext().getSession().put("studentName", studentService.find(student.getStudentID()).getName());
					return "StudentLoginSucceed";
				}
				else{
					addActionMessage("密码错误!请重新输入密码");
					return "LoginFailed";
				}
			}
			else
			{   
				if(studentService.find(student.getStudentID())==null)
					addActionError("不存在此用户!");
				return "LoginFailed";
			}
		}
		
		
	}
	
	public String signout(){
		ActionContext.getContext().getSession().clear();
		addActionMessage("退出成功!");
		return "signout";
	}

}
